package com.travel.server.manager.impl.command.customer.data;

import com.travel.common.util.CollectionUtil;
import com.travel.common.util.CommonUtil;
import com.travel.common.util.DateUtil;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.customer.data.QueryCustomerRepeatBuyResponse;
import com.travel.server.manager.dto.response.customer.data.QueryCustomerRepeatBuyResponse.RepeatItem;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.CustomerDal;
import com.travel.server.manager.model.dto.data.CustomerRepeatDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/10 16:23
 * @Desc:
 */
@Component
@Slf4j
public class QueryCustomerRepeatBuyCommand extends AbstractCommand<EmptyRequest, QueryCustomerRepeatBuyResponse> {

    @Autowired
    private CustomerDal customerDal;

    private final Integer WEEK_INDEX = 5;

    @Override
    protected void checkInput(EmptyRequest request) {

    }

    @Override
    public QueryCustomerRepeatBuyResponse handle(EmptyRequest request) {
        Date endTime = getEndTime();
        Date startTime = getStartTime(endTime);
        List<RepeatItem> repeatList = new ArrayList<>();
        for(int i = 0; i < WEEK_INDEX; i++){
            Date monday = getMonday(startTime, i);
            Date sunday = getSunday(monday);
            String week = getWeek(monday);
            List<CustomerRepeatDto> list = customerDal.selectCustomerRepeat(monday, sunday);
            CustomerRepeatDto customerRepeatDto = CollectionUtil.getFirst(list);
            RepeatItem item = new RepeatItem();
            item.setWeek(week);
            if(customerRepeatDto != null){
                item.setRatio(CommonUtil.getProportion(customerRepeatDto.getOldCustomer(), customerRepeatDto.getTotal()));
            }else {
                item.setRatio("0%");
            }
            repeatList.add(item);
        }
        QueryCustomerRepeatBuyResponse response = new QueryCustomerRepeatBuyResponse();
        response.setRepeatList(repeatList);
        return response;
    }

    private Date getEndTime(){
        Calendar calendar = Calendar.getInstance();
        if(calendar.get(Calendar.DAY_OF_WEEK) == 1) {
            calendar.add(Calendar.DAY_OF_MONTH, -7);
        }
        calendar.set(Calendar.DAY_OF_WEEK, 1);
        return DateUtil.setDayEnd(calendar);
    }

    private Date getStartTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -7 * WEEK_INDEX + 1);
        return DateUtil.setDayStart(calendar);
    }

    private Date getMonday(Date monday, int index){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(monday);
        calendar.add(Calendar.DAY_OF_MONTH, index * 7);
        return DateUtil.setDayStart(calendar);
    }

    private Date getSunday(Date monday){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(monday);
        calendar.add(Calendar.DAY_OF_MONTH,  6);
        return DateUtil.setDayEnd(calendar);
    }

    private String getWeek(Date date){
        int year = DateUtil.getYear(date);
        int week = DateUtil.getWeekNum(date);
        return year + "年第" + week + "周";
    }
}
